$input texcolor, v_fog, v_texcoord0, v_lightmapUV, v_worldPos, v_chunkPos, horizioncolor, cloudscolor, basecolor, ambientcolor

#include <bgfx_shader.sh>
#include <Fuctions_MLYX.glsl>

uniform vec4 RenderChunkFogAlpha;
uniform vec4 FogAndDistanceControl;
uniform vec4 ViewPositionAndTime;
uniform vec4 FogColor;

SAMPLER2D(s_MatTexture,0);
SAMPLER2D(s_SeasonsTexture,1);
SAMPLER2D(s_LightMapTexture,2);

/*====================新的无缝水=====================*/
highp float REFP(highp vec3 Yaa, highp vec3 Yar){
    return ( Yaa.x * Yar.x + Yaa.y * Yar.y + Yaa.z * Yar.z ) / length( Yaa ) / length( Yar );
}

highp float REFV(highp vec3 Eakins){
    Eakins.z += ViewPositionAndTime.w * 1.0;
    float doublePI = 6.28;
    float WX = Eakins.x * doublePI;
    float WY = Eakins.y * doublePI;
    float WZ = Eakins.z * doublePI;
    float TREM1 = sin( ViewPositionAndTime.w * 5.0 + WX * 0.25 - cos( WZ ) );
    float TREM2 = sin( ViewPositionAndTime.w * 5.0 + WX + sin( WZ ) + WZ * 0.5);
    float TREM3 = sin( ViewPositionAndTime.w * 5.0 + WX * 0.5 + sin( WZ ) + WZ * 0.25);
    float TREM4 = sin( ViewPositionAndTime.w * 5.0 + WX * 2.0 + sin( WZ ) + WZ);
    return mix( TREM1, TREM3, TREM2 ) * TREM4;
}

highp vec3 WATTS(highp vec3 Vakins){
    highp float Nonvoi = REFV( Vakins + vec3(0.4, 0.3, 0.2) ) - REFV( Vakins );
    return vec3( -Nonvoi, 0.0, 1.0 );
}

highp float RSPCT(highp vec2 RZ){return fract(cos(RZ.x+RZ.y*1024.0)*1024.0);}
highp float shaderra(highp vec2 RZ){ 
highp vec2 R1=floor(RZ);highp vec2 R2 = fract(RZ);highp vec2 R3 = pow(R2,vec2(3.0,3.0))*(3.0-2.0*R2); 
return mix(mix(RSPCT(R1+vec2(0.0,0.0)),RSPCT(R1+vec2(1.0,0.0)),R3.x),mix(RSPCT(R1+vec2(0.0,1.0)),RSPCT(R1+vec2(1.0,1.0)),R3.x),R3.y);}

highp float rand(highp vec2 RR){return fract(cos(RR.x+RR.y*256.0)*256.0);}
highp float Noiz(highp vec2 RR){ 
highp vec2 UNUM =floor(RR);highp vec2 DOSI =fract(RR);highp vec2 TREZ =pow(DOSI,vec2(3.0,3.0))*(3.0- 2.0*DOSI); 
highp float CUAT =rand(UNUM+vec2(0.0,0.0));highp float CING = rand(UNUM+vec2(1.0,0.0));highp float CINC = rand(UNUM+vec2(0.0,1.0));highp float CINT = rand(UNUM+vec2(1.0,1.0));
highp float RR1=mix(mix(CUAT,CING,TREZ.x),mix(CINC,CINT,TREZ.x),TREZ.y);return RR1;}

highp float nubesmaping(highp vec2 Locatioz, float TIME){
highp float UNUM=1.0;highp float DOSI=0.0;
int TREZ=0;Locatioz+=vec2(2.0,1.0)*TIME*0.000;while(TREZ<4){
DOSI+= shaderra(Locatioz)/UNUM;
UNUM*=1.8;
Locatioz*=2.4;
Locatioz+= TIME*0.00*pow(UNUM,0.1);
Locatioz+= shaderra(Locatioz+TIME*0.0)/UNUM;
TREZ++;
}
return pow(abs(DOSI+0.4),5.0);}


vec4 Normalnubimixum(vec3 locati,float diez,float ragnarokn,float luna,float blue, float TIME) {
vec4 nubediez=vec4(1.3,1.26,1.2,1.3);
vec4 nubeluna=vec4(0.5,0.5,0.5,0.5);
vec4 nuberagnarok=vec4(1.8,1.1,0.3,1.0);
vec4 nubeblue=vec4(0.6,0.6,0.6,1.0);
vec4 nubeapi=mix(nubediez,nuberagnarok,ragnarokn*(1.0-blue));nubeapi=mix(nubeapi,nubeluna,luna);nubeapi=mix(nubeapi,nubeblue,blue);

float anube=nubesmaping(locati.xz,TIME);

vec4 nuberagna=mix(vec4(1.0,1.0,1.0,1.0),nubeapi*0.5,0.5+ragnarokn);nuberagna=mix(vec4(1.0,1.0,1.0,1.0),nubeapi*0.01,luna);
nuberagna.rgb=mix(nuberagna.rgb,nubeapi.rgb*0.8,anube);nuberagna.a=mix(anube,0.0,blue);
return nuberagna;}


void main() {

    vec4 diffuse;
    float time = ViewPositionAndTime.w;//si间
    vec4 FOG_COLOR = FogColor;
    vec3 refpos;//坐标
    refpos.x = v_worldPos.x;
    refpos.y = v_worldPos.y;
    refpos.z = -v_worldPos.z;
    float SampleAlfa = texture2DLod(s_MatTexture, v_texcoord0, 0.0).a;//meiliu fangzi jaujv kuangmufaguang douzit jvncokkelvje sansok
    vec3 SampleColor = texture2D(s_MatTexture, v_texcoord0).rgb;
    vec3 Vultusnormalemvector= normalize(cross(dFdy(v_worldPos),dFdx(v_chunkPos)));
    #define NVY(Vultusnormalemvector) (Vultusnormalemvector.y<-0.8)
    #define NVYB(Vultusnormalemvector) (Vultusnormalemvector.y>0.8)
    #define NVX(Vultusnormalemvector) (Vultusnormalemvector.x<-0.8)
    #define NVXB(Vultusnormalemvector) (Vultusnormalemvector.x>0.8)
    #define NVZ(Vultusnormalemvector) (Vultusnormalemvector.z<-0.8)
    #define NVZB(Vultusnormalemvector) (Vultusnormalemvector.z>0.8)
    #define hell(FOG_COLOR) (FOG_COLOR.r>75.0/255.0&&FOG_COLOR.g<2.0/255.0&&FOG_COLOR.b<2.0/255.0)
    #define mundoe(FOG_COLOR) (FOG_COLOR.r>252.0/255.0&&FOG_COLOR.g>252.0/255.0&&FOG_COLOR.b>252.0/255.0)
    #define pixel(SampleAlfa) (floor(SampleAlfa*255.0+0.0001)==252.0)
    float no=pow(max(min(1.-texcolor.r*1.5,1.0),0.0),1.2);
    float ta=pow(max(min(1.-texcolor.b*1.5,1.0),0.0),0.5);
    bool InsuperiAquoris = FogAndDistanceControl.x==0.0&&FogAndDistanceControl.y<0.8&&(FogColor.b>FogColor.r||FogColor.g>FogColor.r);
    
#if defined(DEPTH_ONLY_OPAQUE) || defined(DEPTH_ONLY)
    diffuse.rgb = vec3(1.0, 1.0, 1.0);
#else
    diffuse.rgb = SampleColor;
    diffuse.a = SampleAlfa;

#if defined(ALPHA_TEST)
    if (diffuse.a < 0.5) {
        discard;
    }
#endif

#if defined(SEASONS) && (defined(OPAQUE) || defined(ALPHA_TEST))
    diffuse.rgb *= mix(vec3(1.0, 1.0, 1.0), texture2D(s_SeasonsTexture, texcolor.xy).rgb * 2.0, texcolor.b);
    diffuse.rgb *= texcolor.aaa;
#else
    diffuse *= texcolor;
#endif
#endif

#ifndef TRANSPARENT
    diffuse.a = 1.0;
#endif

    diffuse.rgb *= texture2D(s_LightMapTexture, v_lightmapUV).rgb;//光照纹理

    float distant = length(v_worldPos.xyz)*0.125;
    float RSS = (1.0 - clamp(( v_lightmapUV.y - 0.91 ) * 30.0, 0.0, 1.0 ));
    float RSG = (1.0 - clamp((texcolor.b - 0.47)*5.0, 0.0, 1.0));
    float LightMap = pow8(v_lightmapUV.x);
    vec3 WhiteColor = vec3(1.0,1.0,1.0);
    vec3 LightColor = vec3(5.0,3.3,1.6);
    vec3 DirectColor = ambientcolor*0.7;
    vec3 ShadowColor = mix(mix(vec3(0.3,0.3,0.3),vec3(0.3,0.3,0.3),ta), vec3(0.5,0.5,0.5), no);

    if(!pixel(SampleAlfa)){
        if(NVX(Vultusnormalemvector) || NVXB(Vultusnormalemvector)){
            diffuse.rgb *= mix( mix(mix(ShadowColor*0.85, DirectColor, no), WhiteColor, RSS), WhiteColor, LightMap);
        }
        diffuse.rgb *= mix(mix(DirectColor, mix(ShadowColor,DirectColor,no), RSS), WhiteColor, LightMap);
        diffuse.rgb *= mix(WhiteColor, LightColor, LightMap);
    }

    #if !defined(ALPHA_TEST) && defined(OPAQUE) && !defined(SEASONS) && !defined(DEPTH_ONLY)
    if(pixel(SampleAlfa)){
        diffuse.rgb *= 4.0;
    }
    #endif 
    //沃特er 安d 安沃特er
    //初始化
    highp float globe;
    float acqua;
    float fresk;

    #ifdef TRANSPARENT
        bool AquaSunt = false;
        if(texcolor.r<texcolor.g&&texcolor.r*1.1<texcolor.b&&texcolor.g<texcolor.b*1.25&&texcolor.b*texcolor.b>texcolor.r*texcolor.g){
            AquaSunt = true;
            acqua = acos(abs(REFP(normalize(v_worldPos),WATTS(v_chunkPos))));
            globe = clamp(max(1.0-FogColor.b*1.0-FogColor.g,0.0),0.0,1.0);
            fresk = clamp(exp(1.0-pow(length(v_worldPos.xz/v_worldPos.y*2.0)*mix(0.08, 0.1, globe),1.0))*0.3, 0.0, 1.0);
            acqua = mix(1.0,acqua,fresk);//假的菲涅尔
            vec4 GraghicNubes = Normalnubimixum(( refpos.xyz/refpos.y )*1.0*acqua,1.0,0.0,0.0,0.0,time);
            vec4 skycolor = mix(vec4(horizioncolor,1.0), mix(vec4(basecolor,0.2),vec4(cloudscolor,0.2),GraghicNubes.a*(v_worldPos.y>0.0?0.0:mix(0.01,0.02,no))),fresk);
            diffuse = mix(skycolor, vec4(0.1,0.2,0.3,0.4), RSS);//室内不应该反射天空
        }
    #endif

    diffuse.rgb = mix(diffuse.rgb, horizioncolor, min(distant*0.01, 1.0));
    diffuse.rgb = mix(diffuse.rgb, horizioncolor, v_fog.a);//Niebula
    
    if(InsuperiAquoris){
        
        #if !defined(TRANSPARENT)
            if(NVY(Vultusnormalemvector)){
                float NebulaAquorisClamp = ( 1.0 - clamp(( v_lightmapUV.y - 0.1 ) * 0.1, 0.0, 1.0 ));
                acqua = acos(abs(REFP(normalize(v_worldPos),WATTS(v_chunkPos))));
                diffuse.rgb = mix(diffuse.rgb+acqua*vec3(0.7,1.2,1.5), diffuse.rgb*vec3(0.85,1.2,1.35), NebulaAquorisClamp );//蓝的钙好喝的钙
            }
        #endif
        if(texcolor.r<texcolor.g&&texcolor.r*1.1<texcolor.b&&texcolor.g<texcolor.b*1.25&&texcolor.b*texcolor.b>texcolor.r*texcolor.g){
            diffuse = mix(vec4(diffuse.rgb*acqua,0.3),vec4(horizioncolor,1.0), v_fog.a);//player卌安沃特er喀时候tai水面喀样
        }
        diffuse.rgb *= mix(0.7,1.0,v_fog.a);
    }
    diffuse.rgb = pow(diffuse.rgb, vec3(2.0,2.0,2.0));
    diffuse.rgb = ACESToneMapping( diffuse.rgb, 1.0 );
    diffuse.rgb = pow(diffuse.rgb, vec3(1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2));
    gl_FragColor = diffuse;
    //world0
    if(hell(FOG_COLOR)){
        vec4 diffuse_hell = texture2D(s_MatTexture, v_texcoord0);
        diffuse_hell *= texcolor;
        diffuse_hell.rgb *= mix(WhiteColor, LightColor, LightMap);
        gl_FragColor = diffuse_hell;
    }
    //world1
    if(mundoe(FOG_COLOR)){
        vec4 diffuse_end = texture2D(s_MatTexture, v_texcoord0);

        #if defined(ALPHA_TEST)
            diffuse_end *= texcolor;
        #endif

        diffuse_end.rgb *= mix(vec3(0.3,0.3,0.5), WhiteColor, LightMap);
        //假光照
        if(NVXB(Vultusnormalemvector)){
            diffuse_end.rgb *= mix(1.5, 1.0, LightMap);
        }
        if(NVY(Vultusnormalemvector)){
            diffuse_end.rgb *= mix(2.5, 1.0, LightMap);
        }
        diffuse_end.rgb *= mix(WhiteColor, vec3(3.0,2.0,3.0), LightMap);

        diffuse_end.rgb = mix(diffuse_end.rgb, EnderNebula(diffuse_end.rgb, v_worldPos), min(distant*0.03, 1.0));
        diffuse_end.rgb = FilmToneMapping(diffuse_end.rgb);
        gl_FragColor = diffuse_end;
    }

    //DEPTH_ONLY_OPAQUE DEPTH_ONLY INSTANCING por la mat je?
    //world2
}